package myJs

import myJs.myPkg._
import myJs.myPkg.jquery._
import myJs.tool.Tool
import org.scalajs.dom.raw.ProgressEvent
import scalatags.Text.all._

import scala.math.BigDecimal.RoundingMode
import scala.scalajs.js.Dynamic.{global => g}
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}
import myJs.Implicits._


/**
 * Created by yz on 2019/3/6
 */
@JSExportTopLevel("Utils")
object Utils {

//  val Dashboard = g.Dashboard.asInstanceOf[Dashboard]

  val checkColumn = ColumnOptions.field("state").checkbox(true)

  val dataToggle = attr("data-toggle")
  val dataContent = attr("data-content")
  val dataContainer = attr("data-container")
  val dataPlacement = attr("data-placement")
  val dataPercent = attr("data-percent")
  val dataHtml = attr("data-html")
  val dataAnimation = attr("data-animation")
  val dataTrigger = attr("data-trigger")
  val dataField = attr("data-field")
  val dataSortable = attr("data-sortable")
  val ariaExpanded = attr("aria-expanded")
  val ariaLabelledby = attr("aria-labelledby")
  val filterDataField = attr("filter-data-field")
  val sortType = attr("sort-type")
  val dataDismiss = attr("data-dismiss")
  val dataValue = attr("data-value")

  def progressHandlingFunction = {
    (e: ProgressEvent) => {
      if (e.lengthComputable) {
        val percent = e.loaded / e.total * 100
        val newPercent = BigDecimal(percent).setScale(2, RoundingMode.HALF_UP)
        $("#progress").html(s"(${newPercent}%)")
        if (percent >= 100) {
          $("#info").text("正在运行")
          $("#progress").html("")
        }
      }
    }
  }

  @JSExport("setColumns")
  def setColumns(value: String,divId:String="") = {
    val parentElem=Tool.getParentElem(divId)
    val element = $(s"${parentElem} input:checkbox[value='${value}']")
    if (element.is(":checked")) {
      $(s"${parentElem} #table").bootstrapTable("showColumn", value)
    } else {
      $(s"${parentElem} #table").bootstrapTable("hideColumn", value)
    }
  }




}
